﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using System.Drawing;
using WebMVC.Utils;

namespace WebMVC.Helpers
{
    public class FileStoreHelper
    {
        private static string storagePath = ConfigurationSettings.AppSettings["fileStorage"];
        private static string fontFilePath = ConfigurationSettings.AppSettings["fontFilePath"];

        public static string GetProdThumbnailPath(long productId, string type)
        {
            string path = "/Img/Product/{0}/{1}/thumb.jpg";
            return String.Format(path, type, productId);
        }

        public static string GetProdDesignPath(long productId, string type)
        {
            string path = "/Img/Product/{0}/{1}/pic.jpg";
            return String.Format(path, type, productId);
        }

        public static string GetLayoutThumbnailPath(long layoutId, string type)
        {
            string path = "/Img/Layout/{0}/{1}/thumb.gif";
            return String.Format(path, type, layoutId);
        }

        public static string GetPaperThumbnailPath(string paperCode)
        {
            string path = "/Img/Paper/{0}/thumb.jpg";
            return String.Format(path, paperCode);
        }

        public static string GetPrintingMethodThumbnailPath(string methodCode)
        {
            string path = "/Img/Printing/{0}/thumb.gif";
            return String.Format(path, methodCode);
        }

        public static void SaveFontFile(string fontFamilyName, HttpPostedFileBase fileBase)
        {
            string folderFullPath = Path.Combine(storagePath, String.Format(fontFilePath, fontFamilyName, ""));
            if (!Directory.Exists(folderFullPath))
            {
                Directory.CreateDirectory(folderFullPath);
            }
            string fullPath = Path.Combine(storagePath, String.Format(fontFilePath, fontFamilyName,fileBase.FileName));
            
            fileBase.SaveAs(fullPath);
        }

        public static string SaveProductTempImage(string path, Guid userId, HttpPostedFileBase fileBase)
        {
            string fullPath = Path.Combine(path, userId.ToString() + ".jpg");
            fileBase.SaveAs(fullPath);
            return userId.ToString() + ".jpg";
        }

        public static void DeleteProductTempFile(string path, Guid userId)
        {
            string filePath = Path.Combine(path, userId.ToString() + ".jpg");
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }

        public static string GetProductTypeImgPath(string p)
        {
            return "/Content/product-img/" + p + ".png";
        }
    }
}
